package com.punch.course.config;


import com.punch.common.feign.FeignOrderService;
import com.punch.course.service.ClassScheduleService;
import com.punch.course.service.CourseClassService;
import com.punch.course.service.StudentCourseScheduleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 课程表定时任务
 */
@Component
@Slf4j
public class ClassScheduleTask {


    @Resource
    private CourseClassService courseClassService;

    @Resource
    private ClassScheduleService classScheduleService;

    @Resource
    private FeignOrderService feignOrderService;

    @Resource
    private StudentCourseScheduleService studentCourseScheduleService;

    /**
     * 班级课程表将该节课变成完成
     */
//    @Scheduled(cron = "59 59 23 * * ?")
//    @Scheduled(cron = "0 0/1 * * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void finishClassSchedule() {
//        //查找已经开班的课程
//        QueryWrapper<PunchClass> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().eq(PunchClass::getClassState, ClassAuditEnum.BEGIN_CLASS.getCode());
//        List<PunchClass> list = classService.list(queryWrapper);
//        LocalDateTime localDateTime = LocalDateTime.now();
//        LocalDateTime localDateTimeBegin = localDateTime.minusDays(1);
//        log.info("localDateTimeBegin--{},localDateTime--{}", localDateTimeBegin, localDateTime);
//        list.forEach(punchClassEX -> {
//            //如果班级课程的课时数等于课表的已完成的课程总数,说明该班级课程已经完成
//            //查找课表
//            UpdateWrapper<ClassSchedule> updateWrapper = new UpdateWrapper<>();
//            updateWrapper.lambda()
//                    .eq(ClassSchedule::getCourseId, punchClassEX.getCourseId())
//                    .eq(ClassSchedule::getClassId, punchClassEX.getId())
//                    .eq(ClassSchedule::getHolidayFlag, 0)
//                    .eq(ClassSchedule::getFinishFlag, 0)
//                    .between(ClassSchedule::getCourseEndDate, localDateTimeBegin, localDateTime);
//            ClassSchedule classSchedule = new ClassSchedule();
//            classSchedule.setFinishFlag(1);
//            boolean update = classScheduleService.update(classSchedule, updateWrapper);
//            if (update) {
//                //课程进度+1
//                classService.updateClassFinishCount(punchClassEX.getId());
//                QueryWrapper<ClassSchedule> scheduleQueryWrapper = new QueryWrapper<>();
//                scheduleQueryWrapper.lambda()
//                        .eq(ClassSchedule::getCourseId, punchClassEX.getCourseId())
//                        .eq(ClassSchedule::getClassId, punchClassEX.getId())
//                        .eq(ClassSchedule::getHolidayFlag, 0)
//                        .eq(ClassSchedule::getFinishFlag, 1);
//                int count = classScheduleService.count(scheduleQueryWrapper);
//                if (count == punchClassEX.getCourseHourCount()) {
//                    //修改课程状态为已完成
//                    PunchClass punchClass = new PunchClass();
//                    punchClass.setClassState(ClassAuditEnum.FINISH_CLASS.getCode());
//                    punchClass.setId(punchClassEX.getId());
//                    classService.updateById(punchClass);
//                }
//                //订单微服务 已完成课时数也需要更新
//                feignOrderService.updateClassFinishCount(punchClassEX.getCourseId(), punchClassEX.getId());
//            }
//
//            //修改该课程下学生签到信息
//
//            UpdateWrapper<StudentCourseSchedule> studentScheduleWrapper = new UpdateWrapper<>();
//            studentScheduleWrapper.lambda()
//                    .eq(StudentCourseSchedule::getClassId, punchClassEX.getId())
//                    .eq(StudentCourseSchedule::getClassDate, localDateTime.toLocalDate())
//                    .eq(StudentCourseSchedule::getStatus, 0);
//            StudentCourseSchedule studentCourseSchedule = new StudentCourseSchedule();
//            studentCourseSchedule.setStatus(3);
//            studentCourseScheduleService.update(studentCourseSchedule, studentScheduleWrapper);

//        });
    }


}
